<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * @package	
 */
class HelpDeskViewManage extends HelpDeskView 
{
	/**
	 * (non-PHPdoc)
	 * @see v1_00/com_helpdesk/site/HelpDeskView#display($tpl)
	 */
	function display($tpl=null) 
	{
		$layout = $this->getLayout();
		switch(strtolower($layout))
		{
			case "view":
			case "form":
				$this->_form($tpl);
			  break;
			case "default":
			default:
				$this->_default($tpl);
			  break;
		}
	}

	/**
	 * 
	 * @param $tpl
	 * @return unknown_type
	 */
	function _default($tpl=null) 
	{
		$mainframe	= HelpDeskConfig::getApplication();
		$user 		= HelpDeskConfig::getUser();
		$database 	= HelpDeskConfig::getDBO();
		$model 		= $this->getModel();
		$app 		= HelpDeskConfig::getApplication();

		// get Itemid
			global $Itemid;
			$this->assignRef( 'Itemid', $Itemid );
			
		// check config		
			$config = &HelpDeskConfig::getInstance();
			$this->assignRef( 'config', $config );

		// If user is not a manager or superadmin, redirect
			$isManager = HelpDeskHelperManager::isUser( $user->id );
			if (!$isManager) {
				$theItemId = HelpDeskHelperHelpDesk::getItemid();
				$link = 'index.php?option='._THISCOMPONENT.'&Itemid='.$theItemId;
				$link = HelpDeskRoute::_( $link, false );
				$msg = HelpDeskText::_( 'You are not authorized to manage tickets' );
				$app->redirect( $link, $msg, 'notice' );
				return;
			}
			
        // Get data from the model
	        $items = $model->getData();
			for ($i=0; $i<count($items); $i++) {
				$items[$i]->categoryname = HelpDeskHelperCategory::getName( (int)$items[$i]->categoryid );
			}
			$this->assignRef( 'items', $items );
			
		// Set the document page title
			$statusid	= $model->getField( $config->getFieldname( 'statusid' ) );
			switch ($statusid) {
				default:
				  $pagetitle = HelpDeskText::_( 'Manage Tickets' );
				  break;
			}
			$this->assignRef( 'pagetitle' , $pagetitle);
			
		// filterlists
			$javascript = 'onchange="document.adminForm.submit();"';
			$filterLists = array();
			$filterLists[] = HelpDeskHelperCategory::getSelectList( $config->getFieldname( 'categoryid' ), $model->getField( $config->getFieldname( 'categoryid' ) ), $javascript );			
			$filterLists[] = HelpDeskHelperStatus::getSelectList( $config->getFieldname( 'statusid' ), $model->getField( $config->getFieldname( 'statusid' ) ), $javascript );
			$this->assignRef('filterLists', $filterLists );
			
		// Assign variables
			$this->assignRef( 'user', $user );
						
		// set search
			$searchterm = $model->getField( $config->getFieldname( 'search' ) );
			$search = "<input type='text' name='".$config->getFieldname( 'search' )."' value='$searchterm' class='text_area' onchange='submitForm(\"".HelpDeskRequest::getVar( 'task' )."\")' />";
			$this->assignRef( 'search' , $search );
			
		// pagenavigation
			$this->assignRef( 'order' , $model->getOrder( 'lastmodified', 'site' ) );
			$this->assignRef( 'order_dir' , $model->getOrderDir( 'desc', 'site' ) );
			$pagination = $model->getPagination( 'site' );    
			$this->assignRef( 'pagination' , $pagination);
		
		// form
			$formAction = "index.php";
			$this->assignRef( 'formAction' , $formAction );
			$validate = josSpoofValue();
			$formValidate = "<input type='hidden' name='{$validate}' value='1' />"; 
			$this->assignRef( 'formValidate' , $formValidate );
			$this->assignRef( 'formOption' , HelpDeskRequest::getVar( 'option' ) );
			$this->assignRef( 'formController' , $this->_name );
			$this->assignRef( 'formId' , $model->_id );
		
		parent::display($tpl);
		
	}
	
	/**
	 * 
	 * @param $tpl
	 * @return unknown_type
	 */	
	function _form($tpl)
	{
		$mainframe	= HelpDeskConfig::getApplication();
		$user 		= HelpDeskConfig::getUser();
		$database 	= HelpDeskConfig::getDBO();
		$model 		= $this->getModel();
		$app 		= HelpDeskConfig::getApplication();

		// get Itemid
			global $Itemid;
			$this->assignRef( 'Itemid', $Itemid );
			
		// check config		
			$config = &HelpDeskConfig::getInstance();
			$this->assignRef( 'config', $config );

        // Get data from the model
			$row = $model->getTable('ticket');
			$row->load( (int) $model->getId() );
			if ($isNew = ($row->id < 1)) { 
				// used for plugins
					$act = 'Edit';
					$pagetitle  = HelpDeskText::_( 'Create Ticket' );
								
				// user for new ticket
					$ticketUser = HelpDeskConfig::getUser( HelpDeskRequest::getVar( 'userid' ) );
					$this->assignRef( 'ticketUser', $ticketUser );
					
				// Get the lists
					$fieldLists = &HelpDeskHelperHelpDesk::buildFieldLists();
					$this->assignRef('fieldLists', $fieldLists);			
			} else {
				if ( !$canView = HelpDeskHelperHelpDesk::canViewTicket( $row->id, $user->id ) ) {
					$link = 'index.php?option='._THISCOMPONENT;
					$link = JRoute::_( $link, false );
					$msg = HelpDeskText::_( 'You are not authorized to view this ticket' );
					$app->redirect( $link, $msg, 'notice' );
		        	return false;
				}
				// used for plugins
				$act = 'View';
				$pagetitle  = HelpDeskText::_( $act.' Ticket' );
				// JToolBarHelper :: deleteList();
	
				// set the category name	
					$row->categoryname = HelpDeskHelperCategory::getName( (int) $row->categoryid );
					
				// get surrounding tickets
					$surrounding = $model->getSurrounding( $row->id );
					$this->assignRef('surrounding', $surrounding);
				
				// get messages
					$messages = HelpDeskHelperTicket::getMessages( $row->id );
					$this->assignRef( 'messages', $messages );
				// get attachments					
					$attachments = HelpDeskHelperHelpDesk::getTicketAttachments( $row->id );
					$this->assignRef( 'attachments', $attachments );
				// get the fields relevant to this category
					$fields = &HelpDeskHelperHelpDesk::getCategoryFields( $row->categoryid );
					$this->assignRef('fields', $fields);
				// get the additional info
					$additional_info = &HelpDeskHelperHelpDesk::getAdditionalData( $row->id );
					$this->assignRef('additional_info', $additional_info);
				// change lists			
					$categories = HelpDeskHelperCategory::getSelectList( 'categoryid', $row->categoryid );
					$this->assignRef( 'categories', $categories );
			}
			
		// set the document page title
			// JToolBarHelper::title(   $pagetitle, 'helpdesk' );
			
		// Assign variables
			$this->assignRef('user', $user );
	        $this->assignRef('row', $row);
	        
		// Get the lists
			$lists = &HelpDeskHelperHelpDesk::buildEditLists();
			$this->assignRef('lists', $lists);
			
		// form
			$formAction = "index.php";
			$this->assignRef( 'formAction' , $formAction );
			$validate = josSpoofValue();
			$formValidate = "<input type='hidden' name='{$validate}' value='1' />"; 
			$this->assignRef( 'formValidate' , $formValidate );
			$this->assignRef( 'formOption' , HelpDeskRequest::getVar( 'option' ) );
			$this->assignRef( 'formController' , $this->_name );
			$this->assignRef( 'formId' , $model->_id );
			$cancelUrl = HelpDeskRoute::_( "index.php?option="._THISCOMPONENT."&controller=tickets&task=list" ); 
			$this->assignRef( 'cancelUrl' , $cancelUrl );

		// fire plugins and display
			$dispatcher	   = HelpDeskHelperPlugin::getInstance();
			$beforeView = $dispatcher->trigger( 'onBefore'.$act.'Ticket', array( &$row, $user ) );
			if (in_array(false, $beforeView, true)) {
				HelpDeskError::raiseError(500, $row->getError());
				return false;
			}
		
			parent::display($tpl);

			$dispatcher->trigger( 'onAfter'.$act.'Ticket', array( $row, $user ) );
	}
	
}